#include "CustomGraphicsView.h"
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QMouseEvent>

CustomGraphicsView::CustomGraphicsView(QWidget *parent)
    : QGraphicsView(parent)
{
}

void CustomGraphicsView::setDrawMode(DrawMode mode)
{
    currentDrawMode = mode;
}

// 辅助函数，用于创建新的椭圆项
QGraphicsEllipseItem* CustomGraphicsView::createEllipseItem(const QRectF &rect, const QPen &pen, const QBrush &brush)
{
    QGraphicsEllipseItem *item = new QGraphicsEllipseItem(rect);
    item->setPen(pen);
    item->setBrush(brush);
    scene()->addItem(item);
    ellipseItems.append(item);
    return item;
}


void CustomGraphicsView::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDrawing = true;
        startPoint = mapToScene(event->pos());

        switch (currentDrawMode) {
        case RectMode:
            // 矩形模式下，初始化矩形的两个角（实际上是同一个点，稍后在move事件中更新）
            rectItem = new QGraphicsRectItem(QRectF(startPoint, QSizeF(0, 0)));
            rectItem->setPen(QPen(Qt::blue)); // 示例颜色
            scene()->addItem(rectItem);
            break;

        case LineMode:
            // 线条模式下，不立即绘制线条，只在move事件中根据起点和当前点绘制
            lineItem = nullptr; // 如果之前有线条，则忽略它（或根据需要重置）
            break;

        case EllipseMode:
            if (!ellipseItem) {
                QRectF ellipseRect(startPoint, QSizeF(0, 0)); // 初始大小为0，稍后在move事件中更新
                ellipseItem = createEllipseItem(ellipseRect, QPen(Qt::green), QBrush(Qt::NoBrush));
            }
            // 否则，不创建新项，只更新startPoint
            break;

        default:
            // ...
            break;
        }

        emit mouseClicked(event->pos());
    }

    QGraphicsView::mousePressEvent(event);
}

void CustomGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
    if (isDrawing) {
        QPointF endPoint = mapToScene(event->pos());

        switch (currentDrawMode) {
        case RectMode:
        {
            QPointF topLeft = QPointF(qMin(startPoint.x(), endPoint.x()), qMin(startPoint.y(), endPoint.y()));
            QPointF bottomRight = QPointF(qMax(startPoint.x(), endPoint.x()), qMax(startPoint.y(), endPoint.y()));
            rectItem->setRect(QRectF(topLeft, bottomRight));
            break;
        }

        case LineMode:
        {
            if (!lineItem) {
                lineItem = scene()->addLine(startPoint.x(), startPoint.y(), endPoint.x(), endPoint.y(), QPen(Qt::red));
            } else {
                lineItem->setLine(QLineF(startPoint, endPoint));
            }
            break;
        }

        case EllipseMode:
        {
            QPointF topLeft = QPointF(qMin(startPoint.x(), endPoint.x()), qMin(startPoint.y(), endPoint.y()));
            QPointF bottomRight = QPointF(qMax(startPoint.x(), endPoint.x()), qMax(startPoint.y(), endPoint.y()));
            QRectF ellipseRect(topLeft, QSizeF(bottomRight.x() - topLeft.x(), bottomRight.y() - topLeft.y()));
            if (ellipseItem) {
                ellipseItem->setRect(ellipseRect);
            }
            break;
        }

        default:
            // 处理默认情况
            break;
        }
    }
    emit mouseMoved(event->pos());
}

void CustomGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && isDrawing) {
        isDrawing = false;
    }

        emit mouseReleased(event->pos());
        //重置模型项，以便下一次重写
        switch (currentDrawMode) {
        case RectMode:
            rectItem = nullptr;
            break;

        case LineMode:
            lineItem = nullptr;
            break;

        case EllipseMode:
            ellipseItem = nullptr;
            break;
    }

    QGraphicsView::mouseReleaseEvent(event);
}
